
version 14
clear
set more off
set matsize 5000
pwd

foreach vers in mech1 nc  {

use ${clean_data}/taxyrcut_parents_small, clear

rename pob_fips pob
drop year
  if "`vers'"=="mech1"  {
  	keep if (inrange(taxyr_cut,1979+1,1979+2) | inrange(taxyr_cut,1984+1,1984+2) | inrange(taxyr_cut,1989+1,1989+2)) ${sc_dropany_miss}
   }
  if "`vers'"=="nc"  {
  	keep if inrange(taxyr_cut,1988,2002) & pob==37
  }

***CLEANUP PARENT TYRC VARS***
do ${main}/dos/tax/build/build_support/kid_parentvar_cleanup_slim.do

*****Merge to Full Parent Data*****
tostring pik, replace
recast str10 pik

merge 1:m pik using ${raw_data}/irs1040_universe_parent_long_t_series.dta, keep(match master) gen(_m_parents) /*NEEDS to include sched_c indicator*/

*keep if _m_parents==3



gen i_obstaxyr = (year==1969 | year==1974 | year==1979 | year==1984 | year==1989 | inrange(year, 1994, 1995) | year>=1998 )

destring pik, replace


 /*merge on decennial race information*/
  merge m:1 pik using ${clean_data}/dec2010_clean, keep(match master) gen(_m_dec2010)
drop _m_dec2010

gen t = year - taxyr_cut
keep if t>=-7
capture drop *e1_pred* *e2_pred*
capture drop *noeitc3* *noeitc4*

if ( "`vers'"=="mech1") {
  	keep if  (t==-7| t==-6| t==-2 | t==-1 | t==3 | t==4 | t==5 | t==7 | t==8 | t==9 | t==10 | t==11 | t==12 | t==13 | t==14 | t==15 | t==16 | t==17 | t==18 | t==19 )
  }
if ("`vers'"=="nc") {
  	keep if inrange(t,-5,16)
   }

  xtset pik year

  gen child_tot = parent_child_home+parent_child_away
  replace child_tot =0 if child_tot==. & parent_have_1040==1

  gen i_married = (parent_fil_stat==2 |parent_fil_stat==3) if parent_have_1040==1
  gen i_married0 = i_married
  replace i_married0 = 0 if i_married==. &  i_obstaxyr==1

  rename parent_have_1040 parent_i_1040

  ***Adjust for outcomes for inflation and create alternate wage measures with zeroes for missing 1040s***
  merge m:1 taxyr_cut using ${supp_data}/eitc_params_yr, keep(match master) keepusing(eitc_in* eitc_out* eitc_max* ) gen(_m_eitc2)
  global ys = "parent_agi parent_wage"
  foreach y of global ys {
  	rename `y' `y'_unadj
  	gen `y' = `y'_unadj*(cpi_2015/cpi_jan)

  	*Cap at 99 percentile (adjusted for inflation)
  	summ `y', detail
  	replace `y' =r(p99) if `y'>r(p99) & `y'!=.
  	replace `y'_unadj =r(p99)*(cpi_jan/cpi_2015) if `y'>r(p99) & `y'!=.

  	gen `y'z = `y'
  	replace `y'z = 0 if `y'==. & parent_i_1040==0
  }
  do ${main}/dos/tax/build/build_support/eitc_elig_mech.do

  ***SHARP BUNCHING INDICATOR**
  gen i_bunch = .

  forvalues k=0/3 {
  	local k1 = "`k'="
  	if `k'==3 local k1="3 <"
  	replace i_bunch = inrange(parent_agiz,eitc_maxcstart`k'*(cpi_2015/cpi_jan)-500, eitc_maxcstart`k'*(cpi_2015/cpi_jan)+500) if `k1'=child_tot
  }

  gen i_bunchse = (i_bunch==1 & parent_sched_c==1) if i_bunch!=.

  drop eitc_in* eitc_out* eitc_max*

  gen hhsize = child_tot + i_married +1
  replace hhsize = 7 if hhsize>=7 & hhsize~=.
  merge m:1 year hhsize using ${supp_data}/povline, keep(match master) gen(_m_pov) keepusing(povline)
  rename povline povline_unadj
  gen povline = povline_unadj*(cpi_jan/cpi_2015)

  gen i_pov = (parent_agiz<povline)
  replace i_pov = . if povline==.

  if "`vers'"=="nc" {
      gen i_frl = (parent_agiz<1.85*povline)
  	  replace i_frl = . if povline==.

      gen i_fl = (parent_agiz<1.30*povline)
      replace i_frl = . if povline==.

      gen i_rl = inrange(parent_agiz,1.30*povline,1.85*povline)
      replace i_rl = . if povline==.


      *EVER FRL ELIGIBLE Grade 3-8 (age 10-14)
      capture drop xx
      gen xx = i_frl if inrange(t,10,14)
      egen ever_frl = max(xx) , by(pik)
      egen always_frl = min(xx) , by(pik)
      capture drop xx
    }


  capture gen tyrc_Lfil_stat0 =tyrc_Lfil_stat
  replace tyrc_Lfil_stat0 =0 if tyrc_L1040!=1

  gen i_pov0 = i_pov
  replace i_pov0 = 1 if i_pov==. &  i_obstaxyr==1

  save ${clean_data}/taxtax_parents_`vers'_clean, replace
}
